package com.foreveross.crawl.web.controller.monitor;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.crawl.common.application.IProxyChannelRelateApplication;
import com.foreveross.crawl.vo.ProxyChannelRelateVO;
import com.foreveross.crawl.web.controller.auth.BaseController;

@Controller
@RequestMapping("/monitor/ProxyIpStatistics")
public class ProxyIpStatisticsController extends BaseController {

	@Inject
	private IProxyChannelRelateApplication iProxyChannelRelateApplication;
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@ResponseBody
	@RequestMapping("/obtainProxyIp")
	public Map<String, Object> obtainProxyIp(@RequestParam(value = "page", required = false)Integer currentPage, @RequestParam(value = "pageSize", required = false) Integer pageSize){
		Map<String, Object> map = new HashMap<String, Object>();
		List<ProxyChannelRelateVO> list = iProxyChannelRelateApplication.getStatisticsByChannel(currentPage, pageSize);
		calcutlateRate(list);
		map.put("Rows", page(currentPage, pageSize, list));
		map.put("Total", list.size());
		return map;
	}

	private List<ProxyChannelRelateVO> page(Integer currentPage, Integer pageSize,
			List<ProxyChannelRelateVO> list) {
		int totalSize = list.size();
		int pageRecord = 0;
		List<ProxyChannelRelateVO> retList = new ArrayList<ProxyChannelRelateVO>();
		if(totalSize <= pageSize){//首页
			retList = list;
		} else {
			pageRecord = (int) ((currentPage - 1) * pageSize);
			for(int i = pageRecord; i < totalSize && i < pageRecord + pageSize; i++){
				retList.add(list.get(i));
			}
		}
		return retList;
	}

	private void calcutlateRate(List<ProxyChannelRelateVO> list) {
		DecimalFormat df = new DecimalFormat("0.00");
		if(list != null){
			for(ProxyChannelRelateVO vo : list){
				Long success = vo.getSuccessNumber();
				if(success == null){
					vo.setSuccessRate("0");
				} else {
					float rate = (vo.getSuccessNumber() / ((float)vo.getSuccessNumber() + (vo.getFailNumber() != null ? (float)vo.getFailNumber() : 0))) * 100;
					vo.setSuccessRate(df.format(rate));
				}
			}
		}
	}
	
	@ResponseBody
	@RequestMapping("/obtainProxyIpGroupByChannel")
	public Map<String, Object> obtainProxyIpGroupByChannel(String ip, @RequestParam(value = "page", required = false)Integer currentPage, @RequestParam(value = "pageSize", required = false) Integer pageSize){
		Map<String, Object> map = new HashMap<String, Object>();
		List<ProxyChannelRelateVO> list = iProxyChannelRelateApplication.getStatisticsByProxyIp(ip, currentPage, pageSize);
		calcutlateRate(list);
		map.put("Rows", page(currentPage, pageSize, list));
		map.put("Total", list.size());
		return map;
	}
}
